package com.xw.service.sysconfig;

import com.xw.common.Const;
import com.xw.dao.DicLimitMapper;
import com.xw.model.DicGroupLimit;
import com.xw.model.DicLimit;
import com.xw.service.common.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(rollbackFor = Exception.class)
public class DicLimitServiceImpl extends BaseServiceImpl<DicLimit> implements DicLimitService {
    private Map map =null;

    @Resource
    private DicLimitMapper limitMapper;
    @Resource
    private DicGroupLimitService groupLimitService;

    @Override
    public List<DicLimit> findByWhere(DicLimit model) {
        return limitMapper.findByWhere(model);
    }

    @Override
    public List<DicLimit> findFirstLevel() {
        return limitMapper.findFirstLevel();
    }

    @Override
    public Map<String, Object> del(String id) {
        map = new HashMap(16);
        DicLimit limit = limitMapper.selectByPrimaryKey(id);
        Integer integer=0;
        /*
                判断是否被使用
                删除的是顶级的菜单
                应该把下面的菜单也一并删除
            */
        // 1.查询该权限是否被使用
        List<DicGroupLimit> groupLimitList= groupLimitService.findByLimitId(id);
        if (!groupLimitList.isEmpty()){
            map.put("code", Const.ERROR_CODE);
            map.put("msg","此权限已被使用，不能删除");
            return map;
        }

        if (Const.PID_ZERO.equals(limit.getLimitPid())){
           // 2.删除子菜单
           limitMapper.delByPid(limit.getLimitId());
           // 3.删除顶级菜单
           integer = limitMapper.deleteByPrimaryKey(id);
        }else {
            /*
                删除的是子级的菜单，直接删除
            */
            integer = limitMapper.deleteByPrimaryKey(id);
        }

        if (integer > 0) {
            map.put("code", Const.SUCCESS_CODE);
            map.put("msg", "删除成功");
        } else {
            map.put("code", Const.ERROR_CODE);
            map.put("msg", "删除失败");
        }
        return map;
    }
}
